/***************************************************************************
 *   Copyright (C) 2006 by Matej Svejda                                    *
 *   mata@aw-modell.at                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "bpmedit.h"
#include "bpmedit.moc"

BPMEdit::BPMEdit(QWidget *parent, const char *name)
	: QLineEdit(parent, name)
{
	timer = new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(checkElapsedTime()));
	calculating = false;
	
	defaultColor = backgroundColor();
	highlightedColor = KApplication::palette().active().highlight().light(200);
	
	connect(this, SIGNAL(returnPressed()), this, SLOT(changeBPM()));
}

BPMEdit::~BPMEdit()
{
}

void BPMEdit::mouseReleaseEvent(QMouseEvent *event)
{
	emit(clicked());
	if(event->button() != Qt::MidButton) {
		return;
	}
	if(!calculating) {
		setBackgroundColor(highlightedColor);
		time.start();
		timer->start(500);
		calculating = true;
	} else {
		bpms.push_back(60000 / time.restart());
		calculateBPM();
	}
}

void BPMEdit::keyReleaseEvent(QKeyEvent *event)
{
	if(event->key() == Qt::Key_Escape) {
		stopCalculating();
	} else {
		event->ignore();
	}
}

void BPMEdit::checkElapsedTime()
{
	if(time.elapsed() > 60000 / min) {
		stopCalculating();
	}
}

void BPMEdit::setBPMRange(int aMin, int aMax)
{
	min = aMin;
	max = aMax;
}

void BPMEdit::calculateBPM()
{
	int sum = 0;
	for(int i = 0; i < bpms.size(); i++) {
		sum += bpms.at(i);
	}

	setText(QString::number(sum / bpms.size()));
}

void BPMEdit::stopCalculating()
{
	setBackgroundColor(defaultColor);
	timer->stop();
	calculating = false;
	bpms.clear();
}

void BPMEdit::changeBPM()
{
	stopCalculating();
	
	int bpm = text().toInt();
	if(bpm < min) {
		bpm = min;
	} else if(bpm > max) {
		bpm = max;
	}
	setText(QString::number(bpm));
	emit(bpmChanged(bpm));
}

void BPMEdit::setBPMSilently(int bpm)
{
	stopCalculating();
	setText(QString::number(bpm));
}
